#include "filereader.h"

/*------------------------------------------------------------------------------
	Constructor
------------------------------------------------------------------------------*/
FileReader::FileReader(){
	m_file=NULL;
}

/*------------------------------------------------------------------------------
	Destructor
------------------------------------------------------------------------------*/
FileReader::~FileReader()
{
}

/*------------------------------------------------------------------------------
	Opens a "filename" config file.
------------------------------------------------------------------------------*/
bool FileReader::init(const char *_filename)
{
	
	m_pLog=&Log::getInstance();
	m_file=fopen(_filename,"r");
	if(m_file==NULL){
		m_pLog->error("Unable to read %s\n",_filename);
		return false;
	}
	
	m_pLog->debug("+ [%s]",_filename);
	return true;
}

/*------------------------------------------------------------------------------
	Closes file opened in init function.
------------------------------------------------------------------------------*/
void FileReader::freeMemory()
{
	if(m_file)
		fclose(m_file);
	m_pLog->debug("- Filereader");
	m_pLog=0;
}

/*------------------------------------------------------------------------------
	Reads a config file line.
------------------------------------------------------------------------------*/
char *FileReader::readLine(int _size)
{

	if(!m_file)
		return NULL;
	
	char *line_readed=new char[_size+1];  

	do{		
		if(!fgets(line_readed,_size,m_file))
		{
			delete line_readed;
			return NULL;
		}
			
	}while(line_readed[0]=='#');

	// Delete "\n" character
	line_readed=strtok(line_readed,"\n");

	return line_readed;
}

/*------------------------------------------------------------------------------
	Reads a config file line.
------------------------------------------------------------------------------*/
char *FileReader::find(const char *_fieldname)
{
	char *line_readed=new char[MAX_LINE_SIZE];
	fseek( m_file, 0L, SEEK_SET );

	do{		
		fgets(line_readed,MAX_LINE_SIZE,m_file);
	}while(!feof(m_file) && (line_readed[0]=='#' || !strstr(line_readed,_fieldname)));
	
	if(!strstr(line_readed,_fieldname))
		line_readed=0;
	
	return line_readed;
}


/*------------------------------------------------------------------------------
	Reads a field, separated by ";", from a config file line
------------------------------------------------------------------------------*/
char *FileReader::readField(const char *_line,int _field_pos)
{
	char *field;
	char *line;
	int i=0;
	
	if(!_line)
	{
		return 0;
	}	
	
	line=new char[MAX_LINE_SIZE];

	strcpy(line,_line);
	field=strtok(line," \n\0");

	while(i<_field_pos && field!=NULL){
		field=strtok(NULL," \n");
		++i;
	}

	return field;
}

/*------------------------------------------------------------------------------
	Reads a field, separated by ";", from a config file line
------------------------------------------------------------------------------*/
char *FileReader::getValue(const char *_fieldname,const int _field_pos)
{
	return readField(find(_fieldname),_field_pos);
}
